package com.mm.cloth.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.mm.cloth.R;

import java.util.List;

public class SeasonAdapter extends RecyclerView.Adapter<SeasonAdapter.ViewHolder> {

    private List<String> seasonList;
    private Context context;
    private OnSeasonClickListener listener;
    private int selectedPosition = 0; // 默认选中第一个（全部）

    public SeasonAdapter(Context context, List<String> seasonList) {
        this.context = context;
        this.seasonList = seasonList;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_season, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        String season = seasonList.get(position);
        holder.tvSeason.setText(season);
        
        // 设置选中状态
        boolean isSelected = position == selectedPosition;
        holder.tvSeason.setSelected(isSelected);
        
        if (isSelected) {
            holder.tvSeason.setBackgroundResource(R.drawable.bg_season_selector);
            holder.tvSeason.setTextColor(context.getResources().getColor(android.R.color.white));
        } else {
            holder.tvSeason.setBackgroundResource(R.drawable.bg_season_selector);
            holder.tvSeason.setTextColor(context.getResources().getColor(android.R.color.darker_gray));
        }
        
        holder.tvSeason.setOnClickListener(v -> {
            if (selectedPosition != position) {
                int previousSelected = selectedPosition;
                selectedPosition = position;
                notifyItemChanged(previousSelected);
                notifyItemChanged(selectedPosition);
                
                if (listener != null) {
                    listener.onSeasonClick(season);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return seasonList == null ? 0 : seasonList.size();
    }

    public void setSeasons(List<String> seasons) {
        this.seasonList = seasons;
        notifyDataSetChanged();
    }

    public String getSelectedSeason() {
        if (seasonList != null && selectedPosition >= 0 && selectedPosition < seasonList.size()) {
            return seasonList.get(selectedPosition);
        }
        return "全部";
    }

    public int getSelectedPosition() {
        return selectedPosition;
    }

    public void setSelectedPosition(int position) {
        if (position >= 0 && position < getItemCount()) {
            int previousSelected = selectedPosition;
            selectedPosition = position;
            notifyItemChanged(previousSelected);
            notifyItemChanged(selectedPosition);
        }
    }

    public void setOnSeasonClickListener(OnSeasonClickListener listener) {
        this.listener = listener;
    }

    public interface OnSeasonClickListener {
        void onSeasonClick(String season);
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        TextView tvSeason;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tvSeason = itemView.findViewById(R.id.tvSeason);
        }
    }
} 